package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.CustomerMapper;
import com.ruoyi.base.domain.Customer;
import com.ruoyi.base.service.ICustomerService;

/**
 * 客户信息
Service业务层处理
 *
 * @author h w
 * @date 2022-12-09
 */
@Service
public class CustomerServiceImpl implements ICustomerService
{
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询客户信息

     *
     * @param customerId 客户信息
主键
     * @return 客户信息

     */
    @Override
    public Customer selectCustomerByCustomerId(Long customerId)
    {
        return customerMapper.selectCustomerByCustomerId(customerId);
    }

    /**
     * 查询客户信息
列表
     *
     * @param customer 客户信息

     * @return 客户信息

     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        if(StringUtils.isNotEmpty(customer.getCustomerType()) && customer.getCustomerType().contains(";")) {
            List<Customer> res = new ArrayList<>();
            String[] split = customer.getCustomerType().split(";");
            for (String s : split) {
                customer.setCustomerType(s);
                res.addAll(customerMapper.selectCustomerList(customer));
            }
            return res;
        }
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 新增客户信息

     *
     * @param customer 客户信息

     * @return 结果
     */
    @Override
    public int insertCustomer(Customer customer)
    {
        customer.setCreateTime(DateUtils.getNowDate());
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户信息

     *
     * @param customer 客户信息

     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer)
    {
        Customer dataCustomer = customerMapper.selectCustomerByCustomerId(customer.getCustomerId());
        if(customer.getSalePrice() != null) {
            customer.setLastSalePrice(dataCustomer.getSalePrice());
            if(customer.getUpdateTime() == null) {
                customer.setUpdateTime(DateUtils.getNowDate());
            }
        }
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户信息

     *
     * @param customerIds 需要删除的客户信息
主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByCustomerIds(Long[] customerIds)
    {
        return customerMapper.deleteCustomerByCustomerIds(customerIds);
    }

    /**
     * 删除客户信息
信息
     *
     * @param customerId 客户信息
主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByCustomerId(Long customerId)
    {
        return customerMapper.deleteCustomerByCustomerId(customerId);
    }
}
